package com.order.controller;


import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import com.order.bean.Admin;
import com.order.service.AdminService;
import com.order.service.impl.AdminServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet(name = "AdminController", value = "/admin/*")
public class AdminController extends BaseController {

    private AdminService adminService = new AdminServiceImpl();

    protected void login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String json = request.getReader().readLine();

        Admin admin = JSON.parseObject(json, Admin.class);

        Admin logined = adminService.login(admin);

        this.writeResultAndData(response, logined);
    }

    protected void add(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String json = request.getReader().readLine();

        Admin admin = JSON.parseObject(json, Admin.class);

        boolean flag = adminService.addAdmin(admin);

        this.writeResult(response, flag);
    }

    protected void deleteAdminById(HttpServletRequest request, HttpServletResponse response) throws IOException {
        Integer id = Integer.parseInt(request.getParameter("id"));

        boolean flag = adminService.deleteById(id);

        this.writeResult(response, flag);
    }

    protected void deleteAdminBatch(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String idsStr = request.getReader().readLine();
        Integer[] ids = JSON.parseObject(idsStr, Integer[].class);
        boolean flag = adminService.deleteBatch(ids);
        this.writeResult(response, flag);
    }

    protected void updateAdmin(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String json = request.getReader().readLine();
        Admin admin = JSON.parseObject(json, Admin.class);
        boolean flag = adminService.updateById(admin);
        this.writeResult(response, flag);
    }

    protected void selectAdmin(HttpServletRequest request, HttpServletResponse response) throws IOException {
        Integer id = Integer.parseInt(request.getParameter("id"));
        Admin admin = adminService.selectById(id);
        this.writeResultAndData(response,admin);
    }

    protected void selectAdminList(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String json = request.getReader().readLine();
        Admin admin = JSON.parseObject(json, Admin.class);
        List<Admin> admins = adminService.selectAll(admin.getUsername());
        this.writeResultAndData(response,admins);
    }

    protected void selectAdminPage(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String name = request.getParameter("name");
        Integer pageNum = Integer.valueOf(request.getParameter("pageNum"));
        Integer pageSize = Integer.valueOf(request.getParameter("pageSize"));
        PageInfo<Admin> adminPageInfo = adminService.selectPage(name, pageNum, pageSize);
        this.writeResultAndData(response,adminPageInfo);
    }

}
